﻿using UnityEngine;



public class GravityEvent
{

    private GameObject gameObject;
    private bool isApplyGravity = false;

    private float GravityParameter = 9.8f;
    private float GravityTime = 0.0f;

    public GravityEvent(GameObject parent)
    {
        gameObject = parent;
        isApplyGravity = true;
        GravityTime = 0.0f;
    }

    public void refreshGravity()
    {
        isApplyGravity = true;
        ActiveObject obj = gameObject.GetComponent<ActiveObject>();
        if (!obj)
            return;

        GravityTime = obj.getObjectTime();

    }

    public void setEnable(bool flag)
    {
        isApplyGravity = flag;
    }

    public float getGravityPoint()
    {
        ActiveObject obj = gameObject.GetComponent<ActiveObject>();
        if (!obj)
            return 0.0f;

        if (!isApplyGravity)
            return 0.0f;

        Rigidbody2D rigidbody = obj.getRigidBody();

        if (rigidbody && rigidbody.bodyType != RigidbodyType2D.Static)
        {
            float subRate = (GravityParameter * (obj.getObjectTime() - GravityTime)) * obj.getWeight();
            return subRate;
        }

        return 0.0f;
    }


}